package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author SunSun
 * @date 2021/10/24
 */
@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser queryWithMaxStore(Long toUserId) {
        Criteria criteria=Criteria.where("toUserId").is(toUserId);

        Query query=Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);

        return mongoTemplate.findOne(query,RecommendUser.class);
    }

    @Override
    public PageResult queryRecommendationList(Integer page, Integer pagesize, Long userId) {

        Criteria criteria=Criteria.where("ToUserId").is(userId);
        Query query=Query.query(criteria);

        long count = mongoTemplate.count(query,RecommendUser.class);

        query.skip((page-1)*pagesize)
                .limit(pagesize).with(Sort.by(Sort.Order.desc("score")));

        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);

        return new PageResult(page,pagesize,count,recommendUsers);
    }

    @Override
    public RecommendUser queryByUserId(Long userId, Long toUserId) {
        Criteria criteria=Criteria.where("userId").is(userId).and("toUserId").is(toUserId);
        Query query=Query.query(criteria);
        RecommendUser one = mongoTemplate.findOne(query, RecommendUser.class);
        if(one==null){
            one=new RecommendUser();
            one.setUserId(userId);
            one.setToUserId(toUserId);
            one.setScore(95d);
        }
        return one;
    }

    @Override
    public List<RecommendUser> queryCardsList(Long userId, int i) {
        List<UserLike> likeList = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)),UserLike.class);

        List<Long> likeUserIds = CollUtil.getFieldValues(likeList, "likeUserId", Long.class);

        Criteria criteria=Criteria.where("userId").nin(likeUserIds).and("toUserId").is(userId);

        TypedAggregation<RecommendUser> newAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),
                Aggregation.sample(i));

        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(newAggregation, RecommendUser.class);


        return results.getMappedResults();
    }
}
